package org.netwia.networks;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.netwia.utils.ValidationUtils;

/**
 * This implementation of input provider takes data from another input provider. 
 * 
 * @author radek.hecl
 *
 * @param <T> type of the network parameter
 */
public class ProxyInputProvider<T> implements InputProvider<T> {

    /**
     * Builder object.
     * 
     * @param <T> type of the network parameter
     */
    public static class Builder<T> {

        /**
         * Source input provider.
         */
        private InputProvider<T> source;
        
        /**
         * Set source input provider.
         * 
         * @param source source input provider
         * @return this instance
         */
        public Builder<T> setSource(InputProvider<T> source) {
            this.source = source;
            return this;
        }
        
        /**
         * Builds the result object.
         * 
         * @return created object
         */
        public ProxyInputProvider<T> build() {
            return new ProxyInputProvider<T>(this);
        }
    }

    /**
     * Source input provider.
     */
    private InputProvider<T> source;

    /**
     * Creates new instance.
     * 
     * @param builder builder object
     */
    public ProxyInputProvider(Builder<T> builder) {
        source = builder.source;
        guardInvariants();
    }

    /**
     * Guards this object to be consistent. Throws exception if this is not the case.
     */
    private void guardInvariants() {
        ValidationUtils.guardNotNull(source, "source cannot be null");
    }

    @Override
    public boolean isNextInputReady() {
        return source.isNextInputReady();
    }

    @Override
    public T getNextInput() {
        return source.getNextInput();
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}
